<?php
/**
 * Created by PhpStorm.
 * User: Stone
 * Date: 2019/11/15
 * Time: 15:12
 */

namespace app\index\service;

use think\facade\Log;

/**
 * 支付相关接口
 * Class PayService
 * @package app\api\service
 */
class PayService{


    /**
     * 微信app支付
     * @param $body
     * @param $out_trade_no
     * @param $total_fee
     * @param $notify_url
     * @return array|false|string
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     */
    public function weAppCreateOrder($body,$out_trade_no,$total_fee,$notify_url){
        $config=config('system.weChatDeveloper2');//开放平台配置
        $wx = new \WeChat\Pay($config);
        //trade_type（公众号和小程序都是JSAPI，同时必须传入openid,如果是app则为APP）
        $option=[
            'body' =>$body,
            'out_trade_no'=>$out_trade_no,
            'total_fee'=>$total_fee*100,//必须乘以100
            'trade_type'=>'APP',
            'notify_url'=>$notify_url,
            'spbill_create_ip'=>request()->ip()
        ];
        // 生成预支付码
        $result = $wx->createOrder($option);
        $result = $wx->createParamsForApp($result['prepay_id']);
        $result = json_encode($result, JSON_UNESCAPED_UNICODE);
        return $result;
    }


    /**
     * 微信支付
     * @param $body
     * @param $out_trade_no
     * @param $total_fee
     * @param $notify_url
     * @param string $openid
     * @return array
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     */
    public function weCreateOrder($body,$out_trade_no,$total_fee,$notify_url,$openid=''){
        $config=config('system.weChatDeveloper');//开放平台配置
        //trade_type（公众号和小程序都是JSAPI，同时必须传入openid,如果是app则为APP）
        $options=[
            'body' =>$body,
            'out_trade_no'=>$out_trade_no,
            'openid'=>$openid,
            'total_fee'=>$total_fee*100,//必须乘以100
            'trade_type'=>'JSAPI',
            'notify_url'=>$notify_url,
            'spbill_create_ip'=>request()->ip()
        ];
        $wechat = \WeChat\Pay::instance($config);
        // 生成预支付码
        $result = $wechat->createOrder($options);
        Log::info('生成预支付码'.json_encode($result));
        // 创建JSAPI参数签名
        $result = $wechat->createParamsForJsApi($result['prepay_id']);
        return $result;
    }


    /**
     * 微信app支付的异步回调验证
     * @return array|bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     */
    public function weNotify(){
        $config=config('system.weChatDeveloper');
        $data=\We::WePayOrder($config)->getNotify();
        if ($data['return_code'] === 'SUCCESS' && $data['result_code'] === 'SUCCESS') {
            //验证成功
            return $data;
        }else{
            return false;
        }
    }


    /**
     * 支付宝app支付创建预支付订单
     * @param $param
     * @param $notify_url
     * @return string
     */
    public function aliPayAppCreateOrder($param,$notify_url){
        $config=config('system.aliDeveloper');
        $config['notify_url'] = $notify_url;
        $aliPay = \We::AliPayApp($config);
        // 请参考（请求参数）：https://docs.open.alipay.com/api_1/alipay.trade.app.pay
        $result = $aliPay->apply($param);
        return $result;
    }


    /**
     * 支付宝回调
     * @return array|bool
     * @throws \WeChat\Exceptions\InvalidResponseException
     */
    public function aliNotify(){
        $config=$config=config('system.aliDeveloper');
        $pay = \We::AliPayApp($config);
        $data = $pay->notify();
        if (in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
            return $data;
        } else {
            return false;
        }
    }


    /**
     * 第三方生成支付链接
     * @param $pay_source
     * @param $title
     * @param $trade_order_id
     * @param $total_fee
     * @param $type
     * @return mixed
     */
    public function thirdPayCreateOrder($pay_source,$title,$trade_order_id,$total_fee,$type){
        if($pay_source === 'aliPay') {
            $url = "http://www.maomaoidc.com/pay/aliPay.php?title={$title}&trade_order_id={$trade_order_id}&total_fee={$total_fee}&type={$type}";
        }else{
            $url = "http://www.maomaoidc.com/pay/wxPay.php?title={$title}&trade_order_id={$trade_order_id}&total_fee={$total_fee}&type={$type}";
        }
        $data=http_get($url);
        Log::info($data);
        $data=json_decode($data,true);
        return $data['data'];//返回跳转的url
    }


    /**
     * 第三方支付回调
     * @param $data
     * @return bool
     */
    public function thirdPayNotify($data){
        $secret=md5('xunwei2022');
        if($secret != $data['secret']){
            return false;
        }
        //这里是验签成功的
        return true;
    }

}